package net.yxsoft.controller.pc.sysmgt;

import com.jfinal.kit.Kv;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Record;
import net.yxsoft.controller.pc.sysmgt.common.AuthController;
import net.yxsoft.resource.SqlRes;
import net.yxsoft.service.LifeService;
import rapid.core.ContextStateEnum;
import rapid.core.TResult;
import rapid.jfinal.Pagination;
import rapid.util.FileKit;

import java.io.File;
import java.util.List;


/**
* @MethodName: LifeController
* @Description:
* @param: null
* @return * @return null
* @throws
* @date 2023/8/31 13:47
* @author jhf
*/
public class LifeController extends AuthController {
  private static final LifeService ser = LifeService.INSTANCE;
  /**
  * @MethodName: index
  * @Description:
  * @param:
  * @return void
  * @throws
  * @date 2023/8/31 13:48
  * @author jhf
  */
  public void index() {
    render("index.html");
  }
  /**
  * @MethodName: getLives
  * @Description:
  * @param:
  * @return void
  * @throws
  * @date 2023/8/31 13:48
  * @author jhf
  */
  public void getLives() {
    String code = getPara("code");
    String title = getPara("title");
    String company = getPara("company");
    String meettype = getPara("meettype");
    String ismodel = getPara("ismodel");

    Kv cond = Kv.create();
    if(StrKit.notBlank(ismodel) && "1".equals(ismodel)) {
      cond.set("B.attrib_02=", code);
    } else {
      cond.set("A.attrib_03=", code);
    }

    if(StrKit.notBlank(title)) {
      cond.set("A.attrib_01 LIKE ", "%" + title + "%");
    }

    if(StrKit.notBlank(company)) {
      cond.set("A.attrib_05=", company);
    }

    if(StrKit.notBlank(meettype)) {
      cond.set("A.attrib_04=", meettype);
    }

    renderJson(ser.query(SqlRes.LIFE + "lives", Kv.by("cond", cond), Pagination.by(this)));
  }
  /**
  * @MethodName: getLifeNodes
  * @Description:
  * @param:
  * @return void
  * @throws
  * @date 2023/8/31 13:48
  * @author jhf
  */
  public void getLifeNodes() {
    String id = getPara("id");
    String userid = getLoginUser().getId();

    Kv cond = Kv.create();
    cond.put("row_id=", id);

    renderJson(ser.getLifeNodes(cond, userid));
  }
  /**
  * @MethodName: getAccTypes
  * @Description:
  * @param:
  * @return void
  * @throws
  * @date 2023/8/31 13:48
  * @author jhf
  */
  public void getAccTypes() {
    String nodeid = getPara("nodeid");

    Kv cond = Kv.create();
    cond.put("A.row_id=", nodeid);

    renderJson(ser.query(SqlRes.LIFE + "lifeacctypes", Kv.by("cond", cond)));
  }
  /**
  * @MethodName: getLifeFils
  * @Description:
  * @param:
  * @return void
  * @throws
  * @date 2023/8/31 13:48
  * @author jhf
  */
  public void getLifeFils() {
    String nodeid = getPara("nodeid");

    Kv cond = Kv.create();
    cond.put("A.row_id=", nodeid);

    renderJson(ser.query(SqlRes.LIFE + "lifefil", Kv.by("cond", cond), Pagination.by(this)));
  }
  /**
  * @MethodName: getLifePer
  * @Description:
  * @param:
  * @return void
  * @throws
  * @date 2023/8/31 13:48
  * @author jhf
  */
  public void getLifePer() {
    String nodeid = getPara("nodeid");
    String userid = getLoginUser().getId();

    result.setState(ContextStateEnum.FAIL);

    Kv cond = Kv.create();
    cond.put("A.row_id=", nodeid);
    cond.put("b.id=", userid);

    List<Record> recs = ser.query(SqlRes.LIFE + "lifeper", Kv.by("cond", cond));

   if(recs.size() <= 0) {
      result.setMessage("您无当前节点的操作权限");

      renderJson(result);
      return;
    }

    result.setState(ContextStateEnum.SUCCESS);
    renderJson(result);
  }
  /**
  * @MethodName: getLifeLimit
  * @Description:
  * @param:
  * @return void
  * @throws
  * @date 2023/8/31 13:48
  * @author jhf
  */
  public void getLifeLimit() {
    String nodeid = getPara("nodeid");

    result.setState(ContextStateEnum.FAIL);

    Kv cond = Kv.create();
    cond.put("A.conflict_id=", nodeid);

    List<Record> recs = ser.query(SqlRes.LIFE + "lifelimit", Kv.by("cond", cond));

    if(recs.size() <= 0 || "0".equals(recs.get(0).getStr("lim"))) {
      result.setMessage("已过当前节点截止日期，不可操作");

      renderJson(result);
      return;
    }

    result.setState(ContextStateEnum.SUCCESS);
    renderJson(result);
  }
  /**
  * @MethodName: saveFils
  * @Description:
  * @param:
  * @return void
  * @throws
  * @date 2023/8/31 13:48
  * @author jhf
  */
  public void saveFils() {
    String nodeid = getPara("nodeid");
    String type = getPara("type");
    String paths = getPara("paths");

    renderJson(ser.saveFils(nodeid, type, paths, getLoginUser()));
  }
  /**
  * @MethodName: deleteFils
  * @Description:
  * @param:
  * @return void
  * @throws
  * @date 2023/8/31 13:48
  * @author jhf
  */
  public void deleteFils() {
    String filids = getPara("ids");

    renderJson(ser.deleteFils(filids, getLoginUser()));
  }
  /**
  * @MethodName: upload
  * @Description:
  * @param:
  * @return void
  * @throws
  * @date 2023/8/31 13:48
  * @author jhf
  */
  public void upload() {
    TResult result=uploadFile();
    renderJson(result);
  }
  /**
  * @MethodName: batchZip
  * @Description:
  * @param:
  * @return void
  * @throws
  * @date 2023/8/31 13:48
  * @author jhf
  */
  public void batchZip() {
    String id = getPara("id");
    String rootPath = getSession().getServletContext().getRealPath("/") + File.separator; //基础路径

    TResult result = ser.batchZip(id, rootPath);
    renderFile(new File(rootPath + result.getData()));
    return;
  }
  /**
  * @MethodName: download
  * @Description:
  * @param:
  * @return void
  * @throws
  * @date 2023/8/31 13:48
  * @author jhf
  */
  public void download() {
    String path = getPara("path");
    String rootPath = getSession().getServletContext().getRealPath("/") + File.separator; //基础路径

    renderFile(new File(rootPath + path));
    return;
  }
}
